package com.jxpowernode.bitcon.utils;

import com.jxpowernode.bitcon.bean.rpcbtc.*;
import com.jxpowernode.bitcon.btcrpccode.*;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchProviderException;
import java.util.Scanner;

/**
 * @Author: Lixuewei
 * @Email:1410511702@qq.com
 * @Desc: 该类用于打印输出比特币方法内容
 * @Date: 2021/12/13 21:19
 */
public class RPCMess {
    /**
     * 该方法用于循环rpc请求的菜单
     * @throws UnsupportedEncodingException
     * @throws NoSuchProviderException
     * @throws IllegalAccessException
     */
    public void getRPCAll() throws UnsupportedEncodingException, NoSuchProviderException, IllegalAccessException {
        Scanner s = new Scanner(System.in);
        while (true) {
            System.out.println("欢迎使用RPC查询方法：");
            System.out.println("获取hash值(1)、获取当前区块的数量(2)、" +
                    "通过高度获取hash值(3)、获取出区块的难度(4)、" +
                    "获取区块信息(5)、返回到上一个菜单(0)");
            System.out.print("请输人你想要看的内容：");
            char n = s.next().charAt(0);
            switch (n) {
                case '1':
                    GetBestBlockHash g = new GetBestBlockHash();
                    System.out.print("输入要查询的id值：");
                    int i = s.nextInt();
                    GetBestBlock b = g.getBestBlockHash(i);
                    System.out.println("id为：" + b.getId() + "、得到的hash值为：" + b.getResult());
                    break;
                case '2':
                    GetBlockCount gt = new GetBlockCount();
                    GetBlockCountHash bc = gt.getBlockCount();
                    System.out.println("得到的hash块的数量为：" + bc.getResult()+"个");
                    break;
                case '3':
                    GetBlockHash hash = new GetBlockHash();
                    System.out.print("输入要查的高度：");
                    int heigt = s.nextInt();
                    int[] arr = new int[]{heigt};;
                    GetBlock blockHash = hash.getBlockHash(arr);
                    System.out.println("id为："+blockHash.getId()+"该高度的hash值为："+blockHash.getResult());
                    break;
                case '4':
                    GetDifficulty difficulty = new GetDifficulty();
                    GetDifficultyHash difficulty1 = difficulty.getDifficulty();
                    System.out.println("获取出区块难度: "+difficulty1.getResult());
                    break;
                case '5' :
                    GetChainTips chainTips = new GetChainTips();
                    ChainResult[] chainTips1 = chainTips.getChainTips();
                    for ( ChainResult t : chainTips1){
                        System.out.println("链头区块的高度："+t.getHeight()
                                +"链头区块的哈希： " +t.getHash()
                                +"链状态："+t.getStatus()
                                +"距离主链分叉点的长度："+t.getBranchlen());
                    }
                    break;
                case '0':
                    System.out.println("退出成功");
                    return;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;
            }
        }
    }
}